function getRandInt(imin, imax) { //[min, max]
    return Math.floor(Math.random() * (imax - imin + 1)) + imin;
}

function getRandFloat(fmin, fmax) { //[min, max)
    return Math.random() * (fmax - fmin) + fmin;
}

function make_auto_complete(selector, source_arr) {
    $(selector).autocomplete({
        source:function( request, response ) {
              var matcher = new RegExp($.ui.autocomplete.escapeRegex( request.term ), "i" );
              response( $.grep( source_arr, function( item ){
                  var v = item.label + item.abbr;
                  return matcher.test(v);
              }) );
          },
        minLength: 0,
        delay: 0,
        autoFocus: true,
    }).autocomplete("instance")._renderItem = function(ul, item) {
        return $("<li>").append("<div style='text-align:left; '>" + item.label + "</div>").appendTo(ul);
    };
}

function get_autoComp_value_by_label(source_arr, label) {
    if(!label) return ""; //undefined?  null? ""?
    for(var i in source_arr) {
        var o = source_arr[i];
        if(label == o.label) {
            return o.v;
        }
    }
    return "NOT FOUND";
}

function set_inputs_elemnt_from_obj(id_prefix, obj) {
    for(var k in obj) {
        if(null == obj[k]) {
            obj[k] = "";
        }
        if("object" == typeof obj[k] || "function" == typeof obj[k] || "undefined" == typeof obj[k]) {
            continue;
        }
        var dstVal = obj[k];
        var cnt = $("#" + id_prefix + k).length;
        if(1 == cnt) {
            var typeStr = $("#" + id_prefix + k).attr("type");
            if("undefined" == typeof typeStr) {
                continue;
            }
            switch(typeStr) {
            case "text":
            case "password":
                $("#" + id_prefix + k).val(dstVal);
                break;
            case "checkbox":
                if("1" == dstVal || 1 == dstVal || true == dstVal || "true" == dstVal) {
                    $("#" + id_prefix + k).prop("checked", true);
                } else {
                    $("#" + id_prefix + k).prop("checked", false);
                }
                break;
            case "radio":
                var setTarget = $("input[name='" + k + "']").filter("[value='" + dstVal + "']");
                if(1 != setTarget.length) {
                    //console.error("id:" + id_prefix + k + ", value:" + dstVal + ", not unique!");
                    continue;
                }
                setTarget.prop("checked", true);
                break;
            }
        } else if (0 == cnt) {
            //radio?
            // if(1 != $("#" + id_prefix + k).length) {
                // continue;
            // }
            // var ty = $("#" + id_prefix + k).type;
            // if(undefined == ty || "radio" != ty) {
                // continue;
            // }
            var setTarget = $("input[name='" + id_prefix + k + "']").filter("[value='" + dstVal + "']");
            if(1 != setTarget.length) {
                //console.error("id:" + id_prefix + k + ", value:" + dstVal + ", not unique!");
                continue;
            }
            setTarget.prop("checked", true);
        }
    }
}

function get_inputs_data(limit_parent_id, key_remove_prefix) {
    var inputObjs = $("#" + limit_parent_id + " input");
    var ret = {};
    for(var k = 0; k < inputObjs.length; ++k) {
        var ty = inputObjs[k].type;
        var skey = "";
        var svalue = "";

        switch(ty) {
        case "text":
        case "password":
            skey = inputObjs[k].id || "";
            svalue = $(inputObjs[k]).val();
            break;
        case "checkbox":
            skey = inputObjs[k].id || "";
            svalue = $(inputObjs[k]).prop("checked");
            svalue = (svalue ? 1 : 0);
            break;
        case "radio":
            skey = inputObjs[k].name || "";
            svalue = $("#" + limit_parent_id + " input[name='" + skey + "']:checked").val();
            break;
        default:
            break;
        }
        
        if("" != skey) {
            if("string" == typeof key_remove_prefix && "" != key_remove_prefix) {
                var pos = skey.indexOf(key_remove_prefix);
                if(-1 != pos) {
                    pos += key_remove_prefix.length;
                    skey = skey.substr(pos);
                }
            }
            ret[skey] = svalue;
        }
    }
    return ret;
}
